package com.iclippi.server.api;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iclippi.server.model.Clipboard;

@SuppressWarnings("serial")
public class ClipboardApi extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		int result=setClipboard(req.getParameter("name"),req.getParameter("content"));
		resp.setContentType("text/html"); 
		resp.setHeader("Pragma", "No-cache");
		resp.setDateHeader("Expires", 0);
		resp.setHeader("Cache-Control", "no-cache");

		PrintWriter out = resp.getWriter();
		out.print(result);
	}
	
	private int setClipboard(String clipboardName, String content){
		if (clipboardName==null) return -1;
		if (content==null) return -2;
		
		Clipboard clipboard = Clipboard.getClipboard(clipboardName);
		clipboard.updateClipboard(content);
		
		return 1;
	}

}
